package com.zzsy.carosshopprojects.activitys.home.mainpage.member.memberconsum.order;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.adapter.ChoseActivityAdapter;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.util.SerializableMap;
import com.zzsy.carosshopprojects.view.refreshview.SpacesItemDecoration;

import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * 消费订单活动列表
 */
public class ActivityListFragment extends BaseFragment {

    public static ActivityListFragment newInstance(SerializableMap map) {
        ActivityListFragment activityListFragment = new ActivityListFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("map", map);
        activityListFragment.setArguments(bundle);
        return activityListFragment;
    }

    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.tv_no_activity)
    TextView tvNoActivity;
    private ActivityListMode mode;
    private Map<String, String> map;
    private ChoseActivityAdapter choseActivityAdapter;

    @Override
    protected int initLayout() {
        return R.layout.fg_activity;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.ActivityCouponList), getActivity());
        choseActivityAdapter = new ChoseActivityAdapter(getContext(), null, 0);
        choseActivityAdapter.setOnItemClickListener((v, position) -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("key", choseActivityAdapter.getListData().get(position));
            setFragmentResult(102, bundle);
            pop();
        });
        recycleView.setLayoutManager(new LinearLayoutManager(context));
        int space = ScreenAdapterTools.getInstance().loadCustomAttrValue(20);
        recycleView.addItemDecoration(new SpacesItemDecoration(space));
        recycleView.setAdapter(choseActivityAdapter);
        tvNoActivity.setOnClickListener(view1 -> {
            Bundle bundle = new Bundle();
            setFragmentResult(103, bundle);
            pop();
        });
    }

    @Override
    protected void initData(Context mContext) {
        mode = getViewModelProvider().get(ActivityListMode.class);
        SerializableMap mapSeriazable = (SerializableMap) getArguments().getSerializable("map");
        map = mapSeriazable.getMap();
    }

    @Override
    protected void loadMethod() {
        getActivityList();
    }


    /**
     * 获取活动折扣
     */
    private void getActivityList() {
        mode.getliveData().observe(this, activityBeans -> {
            choseActivityAdapter.setListData(activityBeans);
            choseActivityAdapter.notifyDataSetChanged();
        });
        mode.getActivityList(getActivity(), map);
    }
}
